package com.nstar.base.util
{
	public class DateUtils
	{
		public static const WEEKS_EN:Array = ["Sunday","Monday","Tuesday","Wednesday","ThursDay","Friday","Saturday"];
		public static const WEEKS_CN:Array = ["星期天","星期一","星期二","星期三","星期四","星期五","星期六"];
		public static const TERRESTRIAL_BRANCH = ["子时","丑时","寅时","卯时","辰时","巳时","午时","未时","申时","酉时","戌时","亥时"];
		/**
		 * 将形如Sat Dec 30 15:58:41 CST 2006的字符串转为Date型
		 **/
		public static function transDate(str:String):Date {
			trace("input: "+str);
			//str = Sat Dec 30 15:58:41 CST 2006
			/* 0:Sat
			1:Dec
			2:30
			3:15:58:41
			4:CST
			5:2006 */
			//new Date("Mon Aug 8 2008 00:00:00");
			
			if(str == "" || str == null) {
				return null;
			}

			var format:String = "";
			var array:Array = str.split(" ");
			
			add(array[0]);//Sat
			add(array[1]);//Dec
			add(array[2]);//30
			add(array[5]);//2006
			add(array[3]);//15:58:41
			
			trace("format: "+format);
			return new Date(format);
			
			function add(item:String):void {
				format += item + " ";
			}
		}
		
		public static function format(date:Date, pattern:String):String {
			if(date == null)
				return "";
			
			pattern = pattern.replace(/YYYY/g, date.getFullYear());
			pattern = pattern.replace(/MM/g, addZero(date.getMonth()+1));
			pattern = pattern.replace(/DD/g, addZero(date.getDate()));
			
			pattern = pattern.replace(/HH/g, addZero(date.getHours()));
			pattern = pattern.replace(/MS/g, addZero(date.getMinutes()));
			pattern = pattern.replace(/SS/g, addZero(date.getSeconds()));
			return pattern;
		}
		
		private static function addZero(input:Number):String {
			if(input<=9 && input >= 0)
				return "0"+input;
			else
				return input.toString();
		}
		
		public static function formatByString(dateStr:String, pattern:String):String {
			
			var date:Date = transDate(dateStr);
			
			return format(date, pattern);
		}
		
		//取得星期
		public static function week(cn:Boolean = true,date:Date=null):String{
			var date:Date =date?date:(new Date());
			var day:Number = date.getDay();
			return cn?WEEKS_CN[day]:WEEKS_EN[day];
		}
		
		//比较日期，返回天数
		public static function compareDay(firstDate:Date,secondDate:Date):int{
			var first:Number = firstDate.getTime();
			var second:Number = secondDate.getTime();
			return (second-first)/(24*60*60*1000);
		}
		
		public static function compare(firstDate:Date,secondDate:Date):Number{
			var first:Number = firstDate.getTime();
			var second:Number = secondDate.getTime();
			return second - first;
		}
		
		//将秒数转换成 hh:mm:ss
		public static function convert(seconds:Number):String{
			var h:int = seconds/3600;
			var m:int = (seconds-h*3600)/60; //整除
			var s:int = (seconds-h*3600)%60;	//求余	
			return 	addZero(h)+":"+addZero(m)+":"+addZero(s);
		}
		
		//比较时间，返回 hh:mm:ss
		public static function CompareTime(firstDate:Date,secondDate:Date):String{
			var value:Number = secondDate.getTime()- firstDate.getTime();
			return convert(value/1000);
		}
		
		//地支
		public static function getTerrestrialBranch():String{
			var time:String = format(new Date(),"HH");
			switch(time){
				case "23","00":
					return TERRESTRIAL_BRANCH[0];
				case "01","02":
					return TERRESTRIAL_BRANCH[1];
				case "03","04":
					return TERRESTRIAL_BRANCH[2];
				case "05","06":
					return TERRESTRIAL_BRANCH[3];
				case "07","07":
					return TERRESTRIAL_BRANCH[4];
				case "09","10":
					return TERRESTRIAL_BRANCH[5];
				case "11","12":
					return TERRESTRIAL_BRANCH[6];
				case "13","14":
					return TERRESTRIAL_BRANCH[7];
				case "15","16":
					return TERRESTRIAL_BRANCH[8];
				case "17","18":
					return TERRESTRIAL_BRANCH[9];
				case "19","20":
					return TERRESTRIAL_BRANCH[10];
				case "21","22":
					return TERRESTRIAL_BRANCH[11];
				default:
					return "";
			}
		}

		//转换时间，结果为月，周，天，时，分，秒
		private static function convertTime(uptime:Number):String 
		{
			if(uptime <= 0) return "已过期！";
			
			var secs:Number = uptime/1000; 
			var result:String = new String();
			
			var months:int = secs/(30 * 24 * 3600);
			secs = secs % (30 * 24 * 3600);
			if(months > 0) result += months + "个月";
			
			var weeks:int = secs/(7 * 24 * 3600);
			secs = secs % (7 * 24 * 3600);
			if(weeks > 0) result += weeks + "周";
			
			var days:int = secs/(24 * 3600);
			secs = secs % (24 * 3600);
			if(days > 0) result += days + "天";
			
			var hours:int = secs/3600;
			secs = secs % 3600;
			if(hours > 0) result += hours + "小时";
			
			var minutes:int = secs/60;
			secs = secs % 60;
			if(minutes > 0) result += minutes + "分钟";
			
			if(Math.round(secs) > 0) result += Math.round(secs) + "秒";
			
			return result;
		}	
		
		private static function convertTime2Array(uptime:Number):Array 
		{
			if(uptime <= 0) return null;
			
			var secs:Number = uptime/1000; 
			var result:Array = new Array();
			
			var totalDay:Number = Math.round(secs/(24*3600));
			result.push({"total":totalDay});
			
			var months:int = secs/(30 * 24 * 3600);
			secs = secs % (30 * 24 * 3600);
			if(months > 0) {
				result.push({"months":months});
			}else{
				result.push({"months":"00"});
			}
			
			var weeks:int = secs/(7 * 24 * 3600);
			secs = secs % (7 * 24 * 3600);
			if(weeks > 0){
				result.push({"weeks":weeks});
			}else{
				result.push({"weeks":"00"});
			}
			
			var days:int = secs/(24 * 3600);
			secs = secs % (24 * 3600);
			if(days > 0){
				result.push({"days":days});
			}else{
				result.push({"days":"00"});
			}
			
			var hours:int = secs/3600;
			secs = secs % 3600;
			if(hours > 0){
				result.push({"hours":hours});
			}else{
				result.push({"hours":"00"});
			}
			
			var minutes:int = secs/60;
			secs = secs % 60;
			if(minutes > 0){
				result.push({"minutes":minutes});
			}else{
				result.push({"minutes":"00"});
			}
			
			if(Math.round(secs) > 0){
				result.push({"secs":Math.round(secs)});
			}else{
				result.push({"secs":"00"});
			}
			
			return result;
		}		
		
		//取得指定时间到当前时间的倒计时
		public static function 	getCountDown(countDownDate:Date):String{
			return convertTime(countDownDate.getTime() - (new Date()).getTime());
		}
		//返回倒计时的数组表现形式
		public static function getCountDown2Array(countDownDate:Date):Array{
			return convertTime2Array(countDownDate.getTime() - (new Date()).getTime());
		}
		
		//日期的中文显示
		public static function monthDayCn(dt:Date=null):String{
			var result:String = "";
			if(!dt){
				dt = new Date();
			}
			var md:String = format(dt,"MM-DD");
			var mds:Array = md.split("-");
			
			var m1:String = String(mds[0]).substr(0,1);
			var m2:String = String(mds[0]).substr(1,1);
			if(m1=="1"){
				result = "十";
			}
			
			result += num2cn(m2)+"月";
			
			var d1:String = String(mds[1]).substr(0,1);
			var d2:String = String(mds[1]).substr(1,1);
			switch(d1){
				case "0":
					result += "";
					break;
				case "1":
					result += "十";
					break;
				case "2":
					result += "二十";
					break;
				case "3":
					result += "三十";
					break;
			}
			result += num2cn(d2)+"号";
			return result;
		}
		
		public static function num2cn(num:String):String{
			switch(num){
				case "0":
					return "";
				case "1":
					return "一";
				case "2":
					return "二";
				case "3":
					return "三";
				case "4":
					return "四";
				case "5":
					return "五";
				case "6":
					return "六";
				case "7":
					return "七";
				case "8":
					return "八";
				case "9":
					return "九";
				default:
					return "";
			}
		}
		
		//时间段
		public static function nowSegment(dt:Date=null):String{
			if(!dt){
				dt = new Date();
			}			
			var time:int = int(format(dt,"HH"));
			if(time>=6 && time<12){
				return "上午";
			}else if(time>=12 && time<13){
				return "中午";
			}else if(time>=13 && time<18){
				return "下午";
			}else if(time>=18 && time<24){
				return "晚上";
			}else{
				return "凌晨";
			}
		}
		
		public static function getDateDiff(startDate:Date, endDate:Date):int   
		{  
		    var diff:Number = (Number(endDate) - Number(startDate))/(3600000*24);  
		    return diff;  
		}  
		
		//获取两个日期之间的月份数
		public static function getMonthDiff(startDate:Date, endDate:Date):int{
		    var diff:Number = (Number(endDate) - Number(startDate))/(3600000*24*30);  
		    return Math.round(diff);  
		}
		
		/* 获得现在日期 */  
		public static function getDate():Number  
		{  
		   return new Number(Date());  
		}  
		
		/* 获得现在日期的月份 */  
		public static function getMonth():Number 
		{  
		   return new Date().getMonth() + 1;  
		}  
		  
		public static function getFullYear():Number  
		{  
		    return new Date().getFullYear();  
		}  
		
		/* 获得给定年的天数 */  
		public static function getDaysOfYear(year:int):int   
		{  
		    var startDate:Date = new Date(year, 0, 1);  
		  var endDate:Date = new Date(year+1, 0, 1);  
		   return (getDateDiff(startDate, endDate));  
		}   
		/* 获得给定年、月的天数 */  
		public static function getDaysOfMonth(year:int, month:int):int 
		{  
		    var startDate:Date = new Date(year, month, 1);  
		    var endDate:Date = new Date(year, month + 1, 1);  
		   return (getDateDiff(startDate, endDate));    
		}  
		//获得给定年 全年月份的天数  
		public static function getMonthlyDayCounts(year:int):Array  
		{  
		    var result:Array = [];  
		    var startDate:Date;  
		    var endDate:Date;  
		    var monthlyDayCount:int;  
		    for (var i:int = 0; i < 12; i++)   
		    {  
		        startDate = new Date(year, i, 1);  
		       endDate = new Date(year, i +1, 1);  
		        monthlyDayCount = getDateDiff(startDate, endDate);   
		        result.push(monthlyDayCount);  
		    }  
		    return result;  
		}  
		
//		/* 比较两个日期大小 */  
//		public static function compare(date1:Date, date2:Date):int   
//		{  
//		   if (Number(date1) < Number(date2))   
//		    {  
//		        return -1  
//		    }  
//		    if (Number(date1) == Number(date2))   
//		    {  
//		       return 0;  
//		   }  
//		   return 1;  
//		}  
		
		//把date日期转化为String  
		public static function convertToDateString(date:Date):String  
		{  
		    var year:String = date.getFullYear().toString();  
		    var month:String = Number(date.getMonth() + 1) < 10 ? "0" + String(date.getMonth() + 1): String(date.getMonth() + 1);  
		    var dates:String = Number(date.getDate()) < 10 ? "0" + String(date.getDate()): String(date.getDate());  
		    return year +"-"+ month+"-"+ dates;  
		} 		
		
	}
}